package cn.com.infohold.smartcity.activity.Publishinfo;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.NewsBean;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class PublishInfoAdd_activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.ll_cb_news)
    LinearLayout ll_cb_news;
    @ViewInject(R.id.ll_cb_policy)
    LinearLayout ll_cb_policy;
    @ViewInject(R.id.ll_cb_laws)
    LinearLayout ll_cb_laws;
    @ViewInject(R.id.cb_news)
    CheckBox cb_news;
    @ViewInject(R.id.cb_policy)
    CheckBox cb_policy;
    @ViewInject(R.id.cb_laws)
    CheckBox cb_laws;
    @ViewInject(R.id.et_title)
    EditText et_title;
    @ViewInject(R.id.et_content)
    EditText et_content;

    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private NewsBean newsBean;
    private int policyType = 0;

    @Override
    protected Object inflateView() {
        return R.layout.activity_add_publish;
    }

    @Override
    protected void initView(View anchor) {

        newsBean = new NewsBean();

        ll_cb_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_news.setChecked(true);
                cb_policy.setChecked(false);
                cb_laws.setChecked(false);
                policyType = 1;
            }
        });

        ll_cb_policy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_policy.setChecked(true);
                cb_news.setChecked(false);
                cb_laws.setChecked(false);
                policyType = 2;
            }
        });

        ll_cb_laws.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_laws.setChecked(true);
                cb_news.setChecked(false);
                cb_policy.setChecked(false);
            }
        });

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (policyType == 0){
                    Toast.makeText(PublishInfoAdd_activity.this, "请选择消息类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (et_title.getText().toString().equals("")){
                    Toast.makeText(PublishInfoAdd_activity.this, "请输入消息标题", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (et_content.getText().toString().equals("")){
                    Toast.makeText(PublishInfoAdd_activity.this, "请输入消息内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                newsBean.setPolicytype(policyType);
                newsBean.setTitle(et_title.getText().toString());
                newsBean.setContent(et_content.getText().toString());
                newsBean.setPublisher(XmlData.getInstance().getUserInfo().getId());
                newsBean.setStatus(1);

                initData();
            }
        });
    }

    private void initData(){
        SDK.api().addPublishInfo(newsBean).postEvent(RequestCode.REQUEST_CODE_ADD_PUBLISHINFO);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent) {
        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_ADD_PUBLISHINFO) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode) {
                case RequestCode.REQUEST_CODE_ADD_PUBLISHINFO:
                    ProgressDialog.dismiss();
                    Type type = new TypeToken<LandResult<Map<String,String>>>() {
                    }.getType();
                    LandResult<Map<String,String>> result = GsonUtil.fromJson(httpEvent.getData(), type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result.getStatus().equals("1")){
                        finish();
                        overridePendingTransitionFade();
                    }

                    break;

                default:
                    break;
            }
        }
    }

}
